package com.csw.shuanfa.utils;

import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;

import java.io.File;

/**
 * @author 1
 */
public class AudioChangeUtil {
    public static void main(String[] args) {

    }

    /**
     * 将一个任何格式掉音频文件转换为wav格式 http://www.sauronsoftware.it/projects/jave/manual.php#9
     *
     * @param sourceUrl 完整源文件路径
     * @param targetUrl 完整目标文件路径
     * @return
     */
    public static void changeToWav(String sourceUrl, String targetUrl) {
        try {
            File source = new File(sourceUrl);
            File target = new File(targetUrl);

            // Audio Attributes
            AudioAttributes audio = new AudioAttributes();
            audio.setCodec("pcm_s16le");
            audio.setBitRate(16000);
            audio.setChannels(1);
            audio.setBitRate(16000);
            audio.setSamplingRate(16000);

            // Encoding attributes
            EncodingAttributes attrs = new EncodingAttributes();
            attrs.setOutputFormat("wav");
            attrs.setAudioAttributes(audio);

            // Encode
            Encoder encoder = new Encoder();
            encoder.encode(new MultimediaObject(source), target, attrs);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
